package com.cpi.cornpp.region.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.region.dto.RegionDTO;
import com.cpi.cornpp.region.exception.RegionException;
import com.cpi.cornpp.region.view.RegionView;

public class RegionConverter {
	
	public List convertDTOsToViews(List regionsDTO)throws RegionException{
		List regionsView = null;
		Iterator iterator = null;
		RegionView regionView = null;
		RegionDTO regionDTO = null;
		try{
			regionsView = new ArrayList(0);
			iterator = regionsDTO.iterator();
			while(iterator.hasNext()){
				regionDTO = (RegionDTO)iterator.next();
				regionView = convertDTOToView(regionDTO);
				regionsView.add(regionView);
			}
		}catch( Exception exception ){
			RegionException regionException=null;
			exception.printStackTrace(System.out);
			regionException= new RegionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw regionException;
		}
		return regionsView;
	}
	
	public RegionView convertDTOToView(RegionDTO regionDTO){
		RegionView regionView = null;
		regionView = new RegionView();
		regionView.setRegionId(regionDTO.getRegionId());
		regionView.setShortName(regionDTO.getShortName());
		regionView.setDescription(regionDTO.getDescription());
		if( regionDTO.getAppDefault() == 1 ){
			regionView.setAppDefault(true);
		}else{
			regionView.setAppDefault(false);
		}
		return regionView;
	}
}